

#pragma once

#include "../DefinesCore.hpp"
#include <memory>
#include <atomic>

CYBERTRON_BEGIN

class SocketEventHandler : public std::enable_shared_from_this<SocketEventHandler>
{
public:
	CybertronCoreAPI SocketEventHandler();
	CybertronCoreAPI virtual ~SocketEventHandler();

	CybertronCoreAPI virtual void onDisposed();
	CybertronCoreAPI virtual void onDisconnected();
	CybertronCoreAPI virtual void onConnected();
	CybertronCoreAPI virtual void onMessage(Message& msg);

public:
	CybertronCoreAPI void setDisposed();
	CybertronCoreAPI bool getDisposed() const;

public:
	CybertronCoreAPI void send(const Message& msg);
	CybertronCoreAPI void send(
		std::uint16_t msgId,
		const google::protobuf::MessageLite& msg);
	CybertronCoreAPI void sendHeartbeat();

	CybertronCoreAPI void close(const char* reason);
	CybertronCoreAPI std::shared_ptr<SocketBase> getSocket() const;

private:
	friend class SocketBase;

	CybertronCoreAPI void setSocket(std::weak_ptr<SocketBase> pSocket);

private:
	std::weak_ptr<SocketBase> mSocket;
	std::atomic<int> mDisposed;
};

CYBERTRON_END
